﻿using BaseData.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BaseData.Infrastructure.Implements
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly BaseDbContext context;

        public BaseRepository(BaseDbContext context)
        {
            this.context = context;
        }
        public async Task<int> AddAll(List<T> values)
        {
            context.Set<T>().AddRange(values);
            return await context.SaveChangesAsync();
        }

        public async Task<int> AddAsync(T entity)
        {
            context.Set<T>().Add(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> DelAsync(int id)
        {
            var res = context.Set<T>().Find(id);
            context.Set<T>().Remove(res);
            return await context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return context.Set<T>();
        }

        public IQueryable<T> GetAlls(Expression<Func<T, bool>> expression)
        {
            return context.Set<T>().Where(expression);
        }

        public async Task<T> GetById(int id)
        {
            return await context.Set<T>().FindAsync(id);
        }

        public async Task<int> UpdateAll(List<T> values)
        {
            context.Set<T>().UpdateRange(values);
            return await context.SaveChangesAsync();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            context.Set<T>().Update(entity);
            return await context.SaveChangesAsync();
        }
    }
}
